//##include('../block/lazyload.js');
//##include('../block/doT.js');
//##include('../block/dropload.js');


var $doc,
    $win,
    $mainContainer,
    $dropDownWrapper,
    $header,
    $fullMaskLoading,
    $newlist,
    $storage,
    $dropDownMsg,
    $dataLoading,
    $noData,
    READ_FLAG = {},
    _threshold = 30,
    endEleTime,
    pageSize = 1;

var _view = {
    init: function() {
        //this.droprefresh();

        this.loadList();
        this.lazyimg();
        this.initReadedStyle($newlist.find('.item'));
    },

    loadList : function(){
        var _this = this;

        if(!$newlist.find('a').last().attr('data-time') || $newlist.find('a').last().attr('data-time') == 'undefined'){
            endEleTime = 0;
        }else{
            endEleTime = $newlist.find('a').last().attr('data-time')
        }

        //$newlist.find('a').last().attr('data-time') == 'undefined' ? (endEleTime = 0) : (endEleTime = $newlist.find('a').last().attr('data-time'));

        $.ajax({
            type: 'GET',
            url: '/welcome/index_apis/newlist?p='+pageSize+'&time='+endEleTime,
            dataType: 'json',
            success: function(data){

                var len = data.list.length;

                if(len > 0){

                    _this.loadlingActive();

                    var temID = $newlist.attr('temId');
                    var tmpl = $("#"+temID).html();
                    var doTtmpl = doT.template(tmpl);

                    $newlist.append(doTtmpl(data));
                    len < 10 ? _this.noDataActive() : _this.loadlingActive();
                    
                    pageSize++;
                    _this.lazyimg();
                    _this.initReadedStyle($newlist.find('.item'));
                }else{
                    _this.noDataActive()
                }
            },
            error: function(){
                console.log('Ajax error!');
            }
        });
    },

    initLoadActive : function(){
        $dropDownWrapper.find('.msg').removeClass('active');
        $dropDownMsg.addClass('active');
    },

    loadlingActive : function(){
        $dropDownWrapper.find('.msg').removeClass('active');
        $dataLoading.addClass('active');
    },

    noDataActive : function(){
        $dropDownWrapper.find('.msg').removeClass('active');
        $noData.addClass('active');
    },

    droprefresh: function(){
        // dropload

        var _this = this;

        $mainContainer.dropload({
            scrollArea : $mainContainer,
            domDown : {
                domClass   : 'dropload-down',
                domRefresh : '<div class="dropload-refresh">上滑加载更多</div>',
                domLoad    : '<div class="dropload-load"><div class="loading"></div>加载中</div>',
                domNoData  : '<div class="dropload-noData">暂无更多内容</div>'
            },
            loadDownFn : function(me){
                var _this = this;

                if(!$newlist.find('a').last().attr('data-time') || $newlist.find('a').last().attr('data-time') == 'undefined'){
                    endEleTime = 0;
                }else{
                    endEleTime = $newlist.find('a').last().attr('data-time')
                }
                $.ajax({
                    type: 'GET',
                    url: '/welcome/index_apis/newlist?p='+pageSize+'&time='+endEleTime,
                    dataType: 'json',
                    success: function(data){
                        var len = data.list.length;

                        if(len > 0){
                            var temID = $newlist.attr('temId');
                            var tmpl = $("#"+temID).html();
                            var doTtmpl = doT.template(tmpl);
                            $newlist.append(doTtmpl(data));

                            pageSize++;

                            _this.lazyimg();
                            _this.initReadedStyle($newlist.find('.item'));
                        }else{
                            me.lock();
                            // 无数据
                            me.noData();
                        }
                        me.resetload();
                    },
                    error: function(){
                        console.log('Ajax error!');
                        me.resetload();
                    }
                });
            },
            autoLoad: true,
            distance:50,
            threshold:0
        });
    },

    initReadedStyle : function($items){
        var _this = this;
        $items.each(function(index,item){
            if(_this.checkReadStatus($(item).attr('data-type'),$(item).attr('data-id'))){
                $(item).find('p').css('color','#5f5f5f');
            }
        })
    },

    checkReadStatus:function(type,id){
        var getReadD = JSON.parse(localStorage.getItem('READ_D'));
        var t = 'T'+ type;

        return (getReadD && getReadD[t] && $.inArray(id,getReadD[t]) != -1) ? true : false;
    },

    clearLocalStorageCache : function(){

    },

    lazyimg:function(){
        $(".lazy").lazyload({
            effect : "show",
            failurelimit : 10,
            threshold : 200
        });
    }
};

var _event = {
    bind: function() {
        $doc.on('click', '.item', this.setReadedColor);
        $win.on('scroll',this.scrollLoad);
        //$mainContainer.on('scroll',this.areaScrollLoad)
    },
    scrollLoad : function(){
        var currScrollT = $win.scrollTop();
        //console.log(currScrollT);

        // 滚动页面触发加载数据
        if(($doc.height() - _threshold) <= (document.documentElement.clientHeight + currScrollT)){
            _view.loadList();
        }
    },

    setReadedColor : function(){
        $(this).find('p').css('color','#5f5f5f');
    }

    //areaScrollLoad : function(){
    //
    //    var currAreaScrollT = $mainContainer.scrollTop();
    //    if(($mainContainer[0].scrollHeight - _threshold) <= ($mainContainer.height() + currAreaScrollT)){
    //        _view.loadList();
    //    }
    //},

};

var _data = {};

function init() {
    _view.init();
    _event.bind();
}

$(document).ready(function(){
    $doc = $(document);
    $win = $(window);
    $header = $('#header');
    $newlist = $('#new-list');
    $fullMaskLoading = $('.full-mask-loading');
    $mainContainer = $('#main-container');
    $dropDownWrapper = $('.drop-down-wrapper');
    $dropDownMsg = $('.drop-down-msg');
    $dataLoading = $('.data-loading');
    $noData = $('.no-data');
    $storage = window.localStorage;

    init();
});
